package com.huixiaodian.ft_user.view.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;

import com.huixiaodian.baselibrary.retrofit.RetrofitClient;
import com.huixiaodian.baselibrary.utils.DefaultTextWatcher;
import com.huixiaodian.baselibrary.utils.ParamProofUtil;
import com.huixiaodian.baselibrary.utils.PhoneNumberUtil;
import com.huixiaodian.baselibrary.view.activity.BaseLibraryActivity;
import com.huixiaodian.ft_user.R;
import com.huixiaodian.ft_user.service.LoginService;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class LoginActivity extends BaseLibraryActivity {


    private Button btn_sendCode, btn_login;
    private EditText et_phone1, et_phone2, et_code, et_password;

    private boolean isSendCodeDone = false;


    private final CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder = stringBuilder.append("重新开始(")
                    .append(millisUntilFinished / 1000).append("s)");
            btn_sendCode.setText(stringBuilder);
        }

        @Override
        public void onFinish() {
            btn_sendCode.setClickable(true);
            btn_sendCode.setTextColor(0xFF61b3fa);
            btn_sendCode.setText("重新发送");
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setContentView() {
        return R.layout.activity_login;
    }

    @Override
    public void initActivityView() {
        super.initActivityView();
        et_code = findViewById(R.id.et_code);
        et_phone1 = findViewById(R.id.et_phone);
        et_phone2 = findViewById(R.id.et_phone2);
        et_password = findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_click);
        btn_sendCode = findViewById(R.id.sendPhoneCode);
        et_phone1.addTextChangedListener(new DefaultTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                btn_sendCode.setEnabled(PhoneNumberUtil
                        .isLengthRight(et_phone1.getText().toString().trim()));
            }
        });
        btn_login.setOnClickListener(onClickListener);
        btn_sendCode.setOnClickListener(onClickListener);
        ((RadioGroup) findViewById(R.id.radioGroup)).setOnCheckedChangeListener(onCheckedChangeListener);
    }

    private final RadioGroup.OnCheckedChangeListener onCheckedChangeListener = (group, checkedId) -> {
        if (group.getId() == R.id.radioGroup) {
            if (checkedId == R.id.radioButton1) {
                //验证码登录
                ((RadioButton) findViewById(R.id.radioButton2)).setTextSize(12.0f);
                findViewById(R.id.loginStyle1).setVisibility(View.VISIBLE);
                findViewById(R.id.loginStyle2).setVisibility(View.GONE);
                String phone = et_phone2.getText().toString().trim();
                if (PhoneNumberUtil.isRight(phone)) et_phone1.setText(phone);


            } else if (checkedId == R.id.radioButton2) {
                //账号登录
                ((RadioButton) findViewById(R.id.radioButton1)).setTextSize(12.0f);
                findViewById(R.id.loginStyle2).setVisibility(View.VISIBLE);
                findViewById(R.id.loginStyle1).setVisibility(View.GONE);
                String phone = et_phone1.getText().toString().trim();
                if (PhoneNumberUtil.isRight(phone)) et_phone2.setText(phone);
            }
        }
    };

    private final View.OnClickListener onClickListener = v -> {
        if (v.getId() == R.id.sendPhoneCode) {
            sendPhoneCode();
        } else if (v.getId() == R.id.btn_click) {
            login();
        }
    };

    private void sendPhoneCode() {
        String phone = et_phone1.getText().toString().trim();
        if(ParamProofUtil.isRightPhone(this, phone)){
            if(!isSendCodeDone) isSendCodeDone = true;
            btn_sendCode.setTextColor(0x8861b3fa);
            btn_sendCode.setClickable(true);
            timer.start();
            Retrofit retrofit = RetrofitClient.getRetrofitClientSaveCookie(this);;
            LoginService loginService = retrofit.create(LoginService.class);
            Call<ResponseBody> call = loginService.sendPhoneCode(phone);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {

                }

                @Override
                public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {

                }
            });
        }
    }


    private void login() {
        if (((RadioButton) findViewById(R.id.radioButton1)).isChecked()) {
            loginStyle1();
        } else if (((RadioButton) findViewById(R.id.radioButton2)).isChecked()) {
            loginStyle2();
        }
    }

    private void loginStyle1() {
        String phone = et_phone1.getText().toString().trim();
        String code = et_code.getText().toString().trim();
        btn_login.setEnabled(false);
        if (ParamProofUtil.isRightPhone(this, phone)
                && ParamProofUtil.isRightCode(this, code)) {
//登录
        } else {
            btn_login.setEnabled(true);
        }
    }

    private void loginStyle2() {
        String phone = et_phone2.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        btn_login.setEnabled(false);
        if (ParamProofUtil.isRightPhone(this, phone)
                && ParamProofUtil.isRightPassword(this, password)) {
            Retrofit retrofit = RetrofitClient.getRetrofitClientSaveCookie(this);
            LoginService loginService = retrofit.create(LoginService.class);
            Call<ResponseBody> call = loginService.loginStyle2(phone, password);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                    try {
                        String responseStr = response.body().string();
                        Log.i("9999999999999", "responseStr =" + responseStr);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
                    Log.i("9999999999999", "reasonStr = " + t.getMessage());
                }
            });

        } else {
            btn_login.setEnabled(true);
        }
    }
}